package com.example.chat.dao

import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
import com.example.chat.model.Message
import com.example.chat.model.MessageStatus
import com.example.chat.model.UserMessage
import com.example.featuresdk.dao.BasicDao
import kotlinx.coroutines.flow.Flow

@Dao
interface MessageDao : BasicDao<Message> {
    /**
     * get all messages for a topic
     */
    @Query("select * from message where topic_id = :topicId order by id asc")
    fun get(topicId: Long): Flow<List<Message>>

    /**
     * get all messages along with the corresponding message sender (User) for a topic
     */
    @Transaction
    @Query("select * from message where topic_id = :topicId order by id asc")
    fun getUserMessage(topicId: Long): Flow<List<UserMessage>>

//    /**
//     * get the last message of a list of topics
//     */
//    @Query("select * from message where topic_id in (:topicIds) group by topic_id, id ")
//    fun getLastMessage(topicIds:List<Long>): Flow<Map<Long, Message>>

    @Query("update message set status = :status where topic_id = :topicId and status <> :status")
    suspend fun updateStatus(status: MessageStatus, topicId: Long)
}